<?php

class Admin_ZhuantiController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$this->view->headScript()->appendFile($this->view->baseUrl().'/js/jquery-1.8.1.min.js');
    }

    public function indexAction()
    {
        // action body
        $db_zhuanti=new Application_Model_DbTable_Zhuanti();
        $zhuanTiList=$db_zhuanti->getAllZhuanTi();
        $this->view->zhuangTiList=$zhuanTiList;
    }
    public function addNewAction(){
    	$form=new Application_Form_NewZhuanTi();
    	$this->view->form=$form;
    	if($this->getRequest()->isPost()){
    		$fromData=$this->getRequest()->getPost();
    		if($form->isValid($fromData)){
    			$imgdata=$this->uploadLogo($form);
    			$data=array(
    				'parent_id'=>$form->getValue('parentId'),
    				'name'=>$form->getValue('title'),
    				'url_spell'=>$form->getValue('url_spell'),
    				'key_words'=>$form->getValue('key_words'),
    				'description'=>$form->getValue('description'),
    				'logo'=>$imgdata,
    				'type'=>$form->getValue('type')
    			);
    		}
    		$db_zhuanti=new Application_Model_DbTable_Zhuanti();
    		$db_zhuanti->addNewZhuanTi($data);
    	}
    }
    private function uploadLogo($form){
    	$month=(string)date('Ym',time());
		$daytime=(string)date('dHi',time());
		$path="./images/zhuanti/".$month."/".$daytime;
		if(!is_dir($path)){
			mkdir($path,0777,true);
		}
		$logo=$form->logo;
		$logo->setDestination($path);
	    $logoName=$form->getValue('logo');
	    $imgdata='/images/zhuanti/'.$month.'/'.$daytime.'/'.$logoName;
	    return $imgdata;
    }
    
    public function setAction(){
    	$id=(int)$this->getRequest()->getParam('id');
    	$db_zhuanTi=new Application_Model_DbTable_Zhuanti();
    	$zhuanTi=$db_zhuanTi->getZhuanTiById($id);
    	$this->view->zhuanTi=$zhuanTi;
    	$config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
		$db = Zend_Db::factory($config->resources->db);
    	if(0==$zhuanTi['type']){
    		$select=$db->select()->from('txy_article',array('id'=>'article_id','title'=>'article_title'))->order('title desc');
    		$res=$db->fetchAll($select);
    	}
    	if(1==$zhuanTi['type']){
    		$select=$db->select()->from('txy_gallery',array('id'=>'gallery_id','title'=>'gallery_title'))->order('title desc');
    		$res=$db->fetchAll($select);
    	}
    	if(2==$zhuanTi['type']){
    		$select=$db->select()->from('txy_taobao_items',array('id'=>'num_iid','title'))->order('title desc');
    		$res=$db->fetchAll($select);
    	}
    	if(3==$zhuanTi['type']){
    		$select=$db->select()->from('txy_zhuanti',array('id','title'=>'name'))->order('title desc');
    		$res=$db->fetchAll($select);
    	}
    	$this->view->mselect=$res;
    }
    
    public function dosetAction(){
    	echo "aaaaaaaaaaaaa";
    	$mselect=$this->getRequest()->getParam('sel_place2');
    	$zhuanTiId=$this->getRequest()->getParam('zhuanTiId');
    	$db_zhuanTiList=new Application_Model_DbTable_ZhuanTiList();
    	foreach ($mselect as $selectedid){
    		$data=array('zhuanti_id'=>$zhuanTiId,'list_id'=>$selectedid);
    		$db_zhuanTiList->addList($data);
    	}
    }


}

